package com.sunspring.zdcall.service.impl;

import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.entity.ZdBaseHospital;
import com.sunspring.zdcall.mapper.master.HospitalMapper;
import com.sunspring.zdcall.param.HospitalParam;
import com.sunspring.zdcall.service.HospitalService;
import com.sunspring.zdcall.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author wulei
 * @date 2019-01-14
 **/
@Service
@Slf4j
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalMapper hm;

    @Override
    public ZdBaseHospital getHospitalInfo() {
        return hm.selectInfo();
    }

    @Override
    public void addHospital(HospitalParam hospital) throws IOException {
        ZdBaseHospital zdBaseHospital = new ZdBaseHospital();
        //保存logo
        String logoName =  FileUtil.saveToLocal(hospital.getLogo());
        BeanUtils.copyProperties(hospital,zdBaseHospital);
        zdBaseHospital.setHospitalLogo(logoName);
        int result = hm.insertSelective(zdBaseHospital);
        if (result <= 0) {
            throw new ZdException("新增医院信息失败");
        }
        log.info("新增医院信息成功");
    }

    @Override
    public void updateHospital(HospitalParam hospital) throws IOException {
        ZdBaseHospital zdBaseHospital = new ZdBaseHospital();
        //是否更新logo
        if (hospital.getLogo() != null) {
            String logoName =  FileUtil.saveToLocal(hospital.getLogo());
            zdBaseHospital.setHospitalLogo(logoName);
        }
        BeanUtils.copyProperties(hospital,zdBaseHospital);
        int result = hm.updateSelective(zdBaseHospital);
        if (result <= 0) {
            throw new ZdException("更新医院信息失败");
        }
        log.info("更新医院信息成功");
    }


}
